package com.nkzy.mapper;

import com.nkzy.pojo.Address;
import org.apache.ibatis.annotations.*;
import java.util.List;


@Mapper
    public interface AddressMapper {

        /**
         * 根据ID查询地址信息
         * @param id 地址ID
         * @return Address对象
         */
        @Select("SELECT * FROM address WHERE id = #{id}")
        Address selectById(Long id);

        /**
         * 根据用户ID查询所有地址信息
         * @param userId 用户ID
         * @return Address对象列表
         */
        @Select("SELECT * FROM address WHERE user_id = #{userId}")
        List<Address> selectByUserId(Long userId);

        /**
         * 根据多个ID查询地址信息
         * @param ids 地址ID列表
         * @return Address对象列表
         */
        @Select("<script>" +
                "SELECT * FROM address WHERE id IN " +
                "<foreach item='id' index='index' collection='list' open='(' separator=',' close=')'>" +
                "#{id}" +
                "</foreach>" +
                "</script>")
        List<Address> selectByIds(List<Long> ids);

        /**
         * 插入新的地址信息
         * @param address Address对象
         * @return 插入的行数
         */
        @Insert("INSERT INTO address (user_id, province, city, town, mobile, street, contact, is_default, notes, deleted) " +
                "VALUES (#{userId}, #{province}, #{city}, #{town}, #{mobile}, #{street}, #{contact}, #{isDefault}, #{notes}, #{deleted})")
        @Options(useGeneratedKeys = true, keyProperty = "id")
        int insert(Address address);

        /**
         * 更新地址信息
         * @param address Address对象
         * @return 更新的行数
         */
        @Update("UPDATE address SET user_id = #{userId}, province = #{province}, city = #{city}, town = #{town}, " +
                "mobile = #{mobile}, street = #{street}, contact = #{contact}, is_default = #{isDefault}, " +
                "notes = #{notes}, deleted = #{deleted} WHERE id = #{id}")
        int update(Address address);

        /**
         * 根据ID删除地址信息
         * @param id 地址ID
         * @return 删除的行数
         */
        @Delete("DELETE FROM address WHERE id = #{id}")
        int deleteById(Long id);
    }

